package com.google.code.dnaep.jdo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

public class PersistenceManagerTest {

	private final LocalServiceTestHelper helper = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

	@Before
	public void setUp() {
		helper.setUp();
	}

	@After
	public void tearDown() {
		helper.tearDown();
	}

	@Test
	public void test() {
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();

		try {
			{
				// TestParent myParent = new TestParent();
				// pm.makePersistent(myParent);
			}

			{
				TestParentObject myParent = new TestParentObject();
				myParent.myChild = new TestChildSubOne();
				pm.makePersistent(myParent);
			}

			{
				// TestSubParentObject myParent = new TestSubParentObject();
				// myParent.myChild = new TestChild();
				// pm.makePersistent(myParent);
			}

			{
				TestParentCollection myParent = new TestParentCollection();
				myParent.myChildren = new ArrayList();
				myParent.myChildren.add(new TestChildSubOne());
				pm.makePersistent(myParent);
			}

			{
				TestParentSubSub myParent = new TestParentSubSub();
				pm.makePersistent(myParent);
			}

			javax.jdo.Query query = pm.newQuery(TestParent.class);

			List<TestParent> results = (List<TestParent>) query.execute();
			assertEquals(3, results.size());

		} finally {
			pm.close();
		}

		assertTrue(true);
	}

	@Test
	public void testGetObjectByIdClassObject() {
		javax.jdo.PersistenceManagerFactory pmf = PMF.get();
		javax.jdo.PersistenceManager pm = pmf.getPersistenceManager();

		try {
			TestParentObject created = new TestParentObject();
			TestParentObject stored = pm.makePersistent(created);

			TestParentObject loaded = pm.getObjectById(null, stored.objectId);
			
			assertNotNull(loaded);
		} finally {
			pm.close();
		}
	}

}
